﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ThresholdAssistant.Tools.Extensions
{
    /// <summary>
    /// 公共的一些编号获取类
    /// </summary>
    public static class ComSerialNoExtensions
    {
        /// <summary>
        /// 获取机器人编号
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static async Task<string> GetRobotSerialNoAsync(this object data)
        {
            if (data == null)
            {
                return "";
            }

            var robotSerialNo = data.GetPropertyValue<string>("robot_serial_no", "RobotSerialNo");//获取机器人编号
            if (string.IsNullOrEmpty(robotSerialNo))
            {
                var robotId = data.GetPropertyValue<string>("robot_id", "RobotId");//机器人Id
                if (!string.IsNullOrEmpty(robotId))
                {
                    //转换成机器人编号
                    robotSerialNo = robotId.ToSerialNo();
                }
            }
            return await Task.FromResult(robotSerialNo);
        }

        /// <summary>
        /// 获取机器人编号
        /// </summary>
        /// <param name="dataList">数据列表</param>
        /// <returns></returns>
        public static async Task<string> GetRobotSerialNoAsync(this IEnumerable<object> dataList)
        {
            var robotSerialNo = string.Empty;
            if (dataList == null || !dataList.Any())
            {
                return "";
            }

            foreach (var data in dataList)
            {
                robotSerialNo = await data.GetRobotSerialNoAsync();
                if (!string.IsNullOrEmpty(robotSerialNo))
                {
                    break;
                }
            }
            return await Task.FromResult(robotSerialNo);
        }

        /// <summary>
        /// 获取机器人编号
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="dataList">解析的数据列表</param>
        /// <returns></returns>
        public static async Task<string> GetRobotSerialNoAsync(this object data, params object[] dataList)
        {
            return await dataList.GetRobotSerialNoAsync();
        }

        /// <summary>
        /// 获取用户编号
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static async Task<string> GetUserSerialNoAsync(this object data)
        {
            if (data == null)
            {
                return "";
            }
            var userSerialNo = data.GetPropertyValue<string>("user_serial_no", "UserSerialNo");//获取用户编号
            return await Task.FromResult(userSerialNo);
        }

        /// <summary>
        /// 获取用户编号
        /// </summary>
        /// <param name="dataList">数据列表</param>
        /// <returns></returns>
        public static async Task<string> GetUserSerialNoAsync(this IEnumerable<object> dataList)
        {
            var userSerialNo = string.Empty;
            if (dataList == null || !dataList.Any())
            {
                return "";
            }

            foreach (var data in dataList)
            {
                userSerialNo = await data.GetUserSerialNoAsync();
                if (!string.IsNullOrEmpty(userSerialNo))
                {
                    break;
                }
            }
            return await Task.FromResult(userSerialNo);
        }

        /// <summary>
        /// 获取用户编号
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="dataList">解析的数据列表</param>
        /// <returns></returns>
        public static async Task<string> GetUserSerialNoAsync(this object data, params object[] dataList)
        {
            return await dataList.GetUserSerialNoAsync();
        }

        /// <summary>
        /// 获取商家编号
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static async Task<string> GetMerchantNoAsync(this object data)
        {
            if (data == null)
            {
                return "";
            }
            return await Task.FromResult(data.GetPropertyValue<string>("merchant_no", "MerchantNo"));
        }

        /// <summary>
        /// 获取商家编号
        /// </summary>
        /// <param name="dataList">数据列表</param>
        /// <returns></returns>
        public static async Task<string> GetMerchantNoAsync(this IEnumerable<object> dataList)
        {
            var merchantNo = string.Empty;
            if (dataList == null || !dataList.Any())
            {
                return "";
            }

            foreach (var data in dataList)
            {
                merchantNo = await data.GetMerchantNoAsync();
                if (!string.IsNullOrEmpty(merchantNo))
                {
                    break;
                }
            }
            return await Task.FromResult(merchantNo);
        }

        /// <summary>
        /// 获取商家编号
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="dataList">解析的数据列表</param>
        /// <returns></returns>
        public static async Task<string> GetMerchantNoAsync(this object data, params object[] dataList)
        {
            return await dataList.GetMerchantNoAsync();
        }

        /// <summary>
        /// 获取透传字段
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static async Task<string> GetExtendAsync(this object data)
        {
            if (data == null)
            {
                return "";
            }
            return await Task.FromResult(data.GetPropertyValue<string>("extend", "Extend"));
        }

        /// <summary>
        /// 获取透传字段
        /// </summary>
        /// <param name="dataList">数据列表</param>
        /// <returns></returns>
        public static async Task<string> GetExtendAsync(this IEnumerable<object> dataList)
        {
            var extend = string.Empty;
            if (dataList == null || !dataList.Any())
            {
                return "";
            }

            foreach (var data in dataList)
            {
                extend = await data.GetExtendAsync();
                if (!string.IsNullOrEmpty(extend))
                {
                    break;
                }
            }
            return await Task.FromResult(extend);
        }

        /// <summary>
        /// 获取透传字段
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="dataList">解析的数据列表</param>
        /// <returns></returns>
        public static async Task<string> GetExtendAsync(this object data, params object[] dataList)
        {
            return await dataList.GetExtendAsync();
        }
    }
}